import os
import pandas
import datetime
import json
if __name__ == '__main__':
    root = '../out/data/datasets/banana-detection/train'

    labels = pandas.read_csv(os.path.join(root, 'label.csv')).to_numpy()

    coco = {
        "info": {
            'year': -1,
            'version': '',
            "description": '', "contributor": '', "url": '',
            "date_created": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        },
        'images': [],
        'annotations': [],
        'licenses': [],
        'categories': []
    }
    images = []
    annotations = []
    for i, l in enumerate(labels):
        x, y, w, h = (l[2] + l[4]) // 2, (l[3] + l[5]) // 2, l[4] - l[2], l[5] - l[3]

        images.append({
            "id": i,
            "width": -1,
            "height": -1,
            "file_name": l[0],
        })
        annotations.append({
            "id": i,
            "image_id": i,
            'category_id': 0,
            "bbox": [x,y,w,h],
            "area":float(w*h),
            "iscrowd":0
        })
    coco['images'] = images
    coco['annotations'] = annotations
    coco['categories'] = [{
        'id':0,
        'name':'banana'
    }]
    f = open('../out/data/datasets/banana-detection/train/annotations.json','w')
    json.dump(coco, f)
    f.close()